package server.maps;

import java.awt.Point;

import client.MapleCharacter;
import handling.world.World;
import server.MapleItemInformationProvider;
import server.Randomizer;
import server.Timer.EventTimer;
import server.life.MapleLifeFactory;
import tools.packet.CField;
import tools.packet.CWvsContext;

public class AramiaFireWorks {

    public final static int KEG_ID = 4001128, SUN_ID = 4001246, DEC_ID = 4001473, 香爐_ID = 4000516;
    public final static int MAX_KEGS = 300, MAX_SUN = 300, MAX_DEC = 300, MAX_香爐 = 300;
    private short kegs = MAX_KEGS / 6;
    private short sunshines = MAX_SUN / 6; //start at 1/6 then go from that
    private short decorations = MAX_DEC / 6;
    private short 香爐 = MAX_香爐 / 6;
    private static final int[] arrayMob = {9500168, 9500169, 9500170, 9500171, 9500173,
        9500174, 9500175, 9500176, 9500170, 9500171, 9500172, 9500173, 9500174, 9500175,
        9400569};
    private static final int[] arrayX = {2100, 2605, 1800, 2600, 3120, 2700, 2320, 2062,
        2800, 3100, 2300, 2840, 2700, 2320, 1950};
    private static final int[] arrayY = {574, 364, 574, 316, 574, 574, 403, 364, 574, 574,
        403, 574, 574, 403, 574};
    private static final int[] array_X = {720, 180, 630, 270, 360, 540, 450, 142,
        142, 218, 772, 810, 848, 232, 308, 142};
    private static final int[] array_Y = {1234, 1234, 1174, 1234, 1174, 1174, 1174, 1260,
        1234, 1234, 1234, 1234, 1234, 1114, 1114, 1140};
    private static final int flake_Y = 149;

    private static final int[] arrayMob_不夜城 = {9410082, 9410083, 9410084, 9410085, 9410086, 9410087, 9410082, 9410083, 9410084, 9410085, 9410086, 9410087, 9410082, 9410083, 9410084, 9410085, 9410086, 9410087};
    private static final int[] arrayX_不夜城 = {1529, 2078, 1817, 1313, 1060, 1845, 1529, 2078, 1817, 1313, 1060, 1845, 1529, 2078, 1817, 1313, 1060, 1845};
    private static final int[] arrayY_不夜城 = {160, 160, 340, 340, 160, 340, 160, 160, 340, 340, 160, 340, 160, 160, 340, 340, 160, 340};

    public final void giveKegs(final MapleCharacter c, final int kegs) {
        this.kegs += kegs;
        if (this.kegs >= MAX_KEGS) {
            this.kegs = 0;
            broadcastEvent(c);
        }
    }

    public final void give香爐(final MapleCharacter c, final int 香爐) {
        this.香爐 += 香爐;
        if (this.香爐 >= MAX_香爐) {
            this.香爐 = 0;
            broadcastEvent_香爐(c);
        }
    }

    private void broadcastServer(final MapleCharacter c, final int itemid) {
        World.Broadcast.broadcastMessage(CWvsContext.serverNotice(6, itemid, "<第" + c.getClient().getChannel() + "頻道> " + c.getMap().getMapName() + " : {" + MapleItemInformationProvider.getInstance().getName(itemid) + "}數量已達到上限。"));
    }

    public final short getKegsPercentage() {
        return (short) (((double) kegs / MAX_KEGS) * 100);
    }

    public final short get香爐Percentage() {
        return (short) (((double) 香爐 / MAX_香爐) * 100);
    }

    private void broadcastEvent(final MapleCharacter c) {
        broadcastServer(c, KEG_ID);
        // Henesys Park
        EventTimer.getInstance().schedule(new Runnable() {

            @Override
            public final void run() {
                startEvent(c.getClient().getChannelServer().getMapFactory().getMap(100000200));
            }
        }, 10000);
    }

    private void broadcastEvent_香爐(final MapleCharacter c) {
        broadcastServer(c, 香爐_ID);
        EventTimer.getInstance().schedule(new Runnable() {

            @Override
            public final void run() {
                startEvent_香爐(c.getClient().getChannelServer().getMapFactory().getMap(741000000));
            }
        }, 10000);
    }

    private void startEvent(final MapleMap map) {
        map.startMapEffect("到底是誰在放煙火啊？", 5121010);

        EventTimer.getInstance().schedule(new Runnable() {

            @Override
            public final void run() {
                spawnMonster(map);
            }
        }, 5000);
    }

    private void startEvent_香爐(final MapleMap map) {
        map.startMapEffect("到底是誰在放煙火啊？", 5121010);

        EventTimer.getInstance().schedule(new Runnable() {

            @Override
            public final void run() {
                spawnMonster_香爐(map);
            }
        }, 5000);
    }

    private void spawnMonster(final MapleMap map) {
        Point pos;
        for (int i = 0; i < arrayMob.length; i++) {
            pos = new Point(arrayX[i], arrayY[i]);
            map.spawnMonsterOnGroundBelow(MapleLifeFactory.getMonster(arrayMob[i]), pos);
        }
    }

    private void spawnMonster_香爐(final MapleMap map) {
        Point pos;
        for (int i = 0; i < arrayMob_不夜城.length; i++) {
            pos = new Point(arrayX_不夜城[i], arrayY_不夜城[i]);
            map.spawnMonsterOnGroundBelow(MapleLifeFactory.getMonster(arrayMob_不夜城[i]), pos);
        }
    }

    public final void giveSuns(final MapleCharacter c, final int kegs) {
        this.sunshines += kegs;
        //have to broadcast a Reactor?
        final MapleMap map = c.getClient().getChannelServer().getMapFactory().getMap(970010000);
        final MapleReactor reactor = map.getReactorByName("mapleTree");
        for (int gogo = kegs + (MAX_SUN / 6); gogo > 0; gogo -= (MAX_SUN / 6)) {
            switch (reactor.getState()) {
                case 0: //first state
                case 1: //first state
                case 2: //first state
                case 3: //first state
                case 4: //first state
                    if (this.sunshines >= (MAX_SUN / 6) * (2 + reactor.getState())) {
                        reactor.setState((byte) (reactor.getState() + 1));
                        reactor.setTimerActive(false);
                        map.broadcastMessage(CField.triggerReactor(reactor, reactor.getState()));
                    }
                    break;
                default:
                    if (this.sunshines >= (MAX_SUN / 6)) {
                        map.resetReactors(); //back to state 0
                    }
                    break;
            }
        }
        if (this.sunshines >= MAX_SUN) {
            this.sunshines = 0;
            broadcastSun(c);
        }
    }

    public final short getSunsPercentage() {
        return (short) (((double) sunshines / MAX_SUN) * 100);
    }

    private void broadcastSun(final MapleCharacter c) {
        broadcastServer(c, SUN_ID);
        // Henesys Park
        EventTimer.getInstance().schedule(new Runnable() {

            @Override
            public final void run() {
                startSun(c.getClient().getChannelServer().getMapFactory().getMap(970010000));
            }
        }, 10000);
    }

    private void startSun(final MapleMap map) {
        map.startMapEffect("楓樹在陽光下散發光芒！", 5121010);
        for (int i = 0; i < 3; i++) {
            EventTimer.getInstance().schedule(new Runnable() {

                @Override
                public final void run() {
                    spawnItem(map);
                }
            }, 5000 + (i * 10000));
        }
    }

    private void spawnItem(final MapleMap map) {
        Point pos;

        for (int i = 0; i < Randomizer.nextInt(10) + 30; i++) {
            pos = new Point(array_X[i], array_Y[i]);
            int itemId = 4001168;
            switch (Randomizer.nextInt(30)) {
                case 0:
                case 1:
                    itemId = 3010141;
                    break;
                case 2:
                    itemId = 3010146;
                    break;
                case 3:
                case 4:
                    itemId = 3010025;
                    break;
            }
            map.spawnAutoDrop(itemId, pos);
        }
    }

    public final void giveDecs(final MapleCharacter c, final int kegs) {
        this.decorations += kegs;
        //have to broadcast a Reactor?
        final MapleMap map = c.getClient().getChannelServer().getMapFactory().getMap(555000000);
        final MapleReactor reactor = map.getReactorByName("XmasTree");
        for (int gogo = kegs + (MAX_DEC / 6); gogo > 0; gogo -= (MAX_DEC / 6)) {
            switch (reactor.getState()) {
                case 0: //first state
                case 1: //first state
                case 2: //first state
                case 3: //first state
                case 4: //first state
                    if (this.decorations >= (MAX_DEC / 6) * (2 + reactor.getState())) {
                        reactor.setState((byte) (reactor.getState() + 1));
                        reactor.setTimerActive(false);
                        map.broadcastMessage(CField.triggerReactor(reactor, reactor.getState()));
                    }
                    break;
                default:
                    if (this.decorations >= MAX_DEC / 6) {
                        map.resetReactors(); //back to state 0
                    }
                    break;
            }
        }
        if (this.decorations >= MAX_DEC) {
            this.decorations = 0;
            broadcastDec(c);
        }
    }

    public final short getDecsPercentage() {
        return (short) (((double) decorations / MAX_DEC) * 100);
    }

    private void broadcastDec(final MapleCharacter c) {
        broadcastServer(c, DEC_ID);
        EventTimer.getInstance().schedule(new Runnable() {

            @Override
            public final void run() {
                startDec(c.getClient().getChannelServer().getMapFactory().getMap(555000000));
            }
        }, 10000); //no msg
    }

    private void startDec(final MapleMap map) {
        map.startMapEffect("白色聖誕節之丘開始下雪了！", 5120000);
        for (int i = 0; i < 3; i++) {
            EventTimer.getInstance().schedule(new Runnable() {

                @Override
                public final void run() {
                    spawnDec(map);
                }
            }, 5000 + (i * 10000));
        }
    }

    private void spawnDec(final MapleMap map) {
        Point pos;

        for (int i = 0; i < Randomizer.nextInt(10) + 40; i++) {
            pos = new Point(Randomizer.nextInt(800) - 400, flake_Y);
            map.spawnAutoDrop(Randomizer.nextInt(15) == 1 ? 4310012 : 4310011, pos);
        }
    }
}
